import * as _ from './common';
import src from './source';

//新版公式注册入口
export const registerFormula = () => {
    const { commonParam,baseParam } = src.globalStore;
    const { layouttype,modeid, ismode, signatureAttributesStr, signatureSecretKey, wfTestStr } = commonParam;
    const reqParams = {...baseParam, ...{layouttype,modeid, signatureAttributesStr, signatureSecretKey, wfTestStr}};
    if (ismode == 2) {
        src.API_REQ.getFormulasNew(reqParams).then(result => {
            const datas = result.data;
            const systemdata = result.systemdata;
            bindFormulaEvent(datas, systemdata);
        });
    }
}

//绑定公式事件
export const bindFormulaEvent = (datas, systemdata) => {
    const { dataJson,fieldAttrMap,mainData,detailMap } = src.layoutStore;
    const { importDetail } = src.globalStore.urlParams;
    if (datas) {
        datas.map(data => {
            //formula:"[sys.-1]=1",formuladesc:"[系统.请求标题]=1",formulakey:"1533908594972#new#",
            //name:"",triggerdesc:"值改变时 (触发字段: 主表.申请人)##载入时",triggers:"1#21934##21",
            try {
                //1#21934##21
                const triggers = data.triggers;
                if (triggers) {
                    //1#21934
                    //21
                    let triggersArray = triggers.split("##");
                    triggersArray && triggersArray.map(trigger => {
                        let triggerArray = trigger.split("#");
                        if (triggerArray && triggerArray.length > 0 && triggerArray[0]) {
                            //触发动作
                            const action = triggerArray[0];
                            //触发字段
                            let fieldid = "";
                            let tableMark = "";
                            let fieldMark = "";
                            if (triggerArray.length > 1) {
                                fieldid = triggerArray[1];
                                tableMark = fieldAttrMap.get(fieldid).tableMark;
                                fieldMark = `field${fieldid}`;
                            }
                            
                            let triInfo = {};
                            if (data.formulatxt) {
                                triInfo.formulatxt = eval(data.formulatxt);
                            }
                            triInfo.formulakey = data.formulakey;
                            if (data.destcell) {
                                triInfo.destcell = eval(data.destcell);
                            }
                            if (data.destfield) {
                                triInfo.destfield = eval(data.destfield);
                            }
                            if (data.cellrange) {
                                triInfo.cellrange = eval(data.cellrange);
                            }
                            let triggerGroupid;
                            if (tableMark && tableMark.indexOf("_") >= 0) {
                                triggerGroupid = tableMark.substring(tableMark.indexOf("_") + 1);
                            }
                            triInfo.triggerGroupid = triggerGroupid;
                            triInfo.systemdata = systemdata;

                            triInfo.workflowid = src.globalStore.commonParam.workflowid;
                            triInfo.formid = src.globalStore.commonParam.formid;
                            triInfo.isbill = src.globalStore.commonParam.isbill;
                            triInfo.requestid = src.globalStore.commonParam.requestid;

                            //值改变时
                            if (action == 1) {
                                if(tableMark.indexOf("detail") === -1) {//主表
                                    //(主字段变更)
                                    window.WfForm.bindFieldChangeEvent(fieldMark, function(obj,id,value){
                                        triInfo.newValue = value;
                                        triInfo.triggerFieldid = id;
                                        //调用formulaNew_wev8.js中的formulaTrigger方法
                                        window.formulaTrigger(triInfo);
                                    });
                                } else {//明细表
                                    //(明细字段变更)
                                    window.WfForm.bindDetailFieldChangeEvent(fieldMark, function(id,rowIndex,value){
                                        triInfo.newValue = value;
                                        triInfo.triggerFieldid = id;
                                        triInfo.triggerRowIndex = rowIndex;
                                        window.formulaTrigger(triInfo);
                                    });
                                    //明细导入后表单加载时，所有触发字段为明细字段的公式都执行
                                    if (importDetail == "1") {
                                        triInfo.triggerFieldid = fieldid;
                                        triInfo.importDetail = '1';
                                        window.formulaTrigger(triInfo);
                                    }
                                }
                            }
                            //获取输入焦点时
                            else if (action == 2) {
                                window.WfForm.bindFieldAction('onfocus', fieldMark, function(id,rowIndex){
                                    triInfo.triggerFieldid = id;
                                    triInfo.triggerRowIndex = rowIndex;
                                    window.formulaTrigger(triInfo);
                                });
                            }
                            //失去输入焦点时
                            else if (action == 3) {
                                window.WfForm.bindFieldAction('onblur', fieldMark, function(id,rowIndex){
                                    triInfo.triggerFieldid = id;
                                    triInfo.triggerRowIndex = rowIndex;
                                    window.formulaTrigger(triInfo);
                                });
                            }
                            //鼠标单击时
                            else if (action == 4) {
                                window.WfForm.bindFieldAction('onclick', fieldMark, function(id,rowIndex){
                                    triInfo.triggerFieldid = id;
                                    triInfo.triggerRowIndex = rowIndex;
                                    window.formulaTrigger(triInfo);
                                });
                            }
                            //鼠标双击时
                            else if (action == 5) {
                                window.WfForm.bindFieldAction('ondbclick', fieldMark, function(id,rowIndex){
                                    triInfo.triggerFieldid = id;
                                    triInfo.triggerRowIndex = rowIndex;
                                    window.formulaTrigger(triInfo);
                                });
                            }
                            //鼠标移入时
                            else if (action == 6) {
                                window.WfForm.bindFieldAction('mouseover', fieldMark, function(id,rowIndex){
                                    triInfo.triggerFieldid = id;
                                    triInfo.triggerRowIndex = rowIndex;
                                    window.formulaTrigger(triInfo);
                                });
                            }
                            //鼠标移出时
                            else if (action == 7) {
                                window.WfForm.bindFieldAction('mouseout', fieldMark, function(id,rowIndex){
                                    triInfo.triggerFieldid = id;
                                    triInfo.triggerRowIndex = rowIndex;
                                    window.formulaTrigger(triInfo);
                                });
                            }
                            //载入时
                            else if (action == 21) {
                                window.formulaTrigger(triInfo);
                            }
                            //窗口滚动时
                            else if (action == 22) {
                                window.WfForm.bindScrollEvent(function(){
                                    window.formulaTrigger(triInfo);
                                });
                            }
                            //提交/批准时
                            else if (action == 51) {
                                window.WfForm.registerCheckEvent(window.WfForm.OPER_SUBMIT, function(callback){
                                    let ret = window.formulaTrigger(triInfo);
                                    if (ret) {
                                        window.WfForm.showMessage(ret, 1, 5);
                                    } else {
                                        callback();
                                    }
                                });
                            }
                            //保存时
                            else if (action == 52) {
                                window.WfForm.registerCheckEvent(window.WfForm.OPER_SAVE, function(callback){
                                    let ret = window.formulaTrigger(triInfo);
                                    if (ret) {
                                        window.WfForm.showMessage(ret, 1, 5);
                                    } else {
                                        callback();
                                    }
                                });
                            }
                            //退回时
                            else if (action == 53) {
                                window.WfForm.registerCheckEvent(window.WfForm.OPER_REJECT, function(callback){
                                    let ret = window.formulaTrigger(triInfo);
                                    if (ret) {
                                        window.WfForm.showMessage(ret, 1, 5);
                                    } else {
                                        callback();
                                    }
                                });
                            }
                            //转办时
                            else if (action == 54) {
                                window.WfForm.registerCheckEvent(window.WfForm.OPER_TURNHANDLE, function(callback){
                                    let ret = window.formulaTrigger(triInfo);
                                    if (ret) {
                                        window.WfForm.showMessage(ret, 1, 5);
                                    } else {
                                        callback();
                                    }
                                });
                            }
                            //转发时
                            else if (action == 56) {
                                window.WfForm.registerCheckEvent(window.WfForm.OPER_FORWARD, function(callback){
                                    let ret = window.formulaTrigger(triInfo);
                                    if (ret) {
                                        window.WfForm.showMessage(ret, 1, 5);
                                    } else {
                                        callback();
                                    }
                                });
                            }
                            //意见征询时
                            else if (action == 57) {
                                window.WfForm.registerCheckEvent(window.WfForm.OPER_ASKOPINION, function(callback){
                                    let ret = window.formulaTrigger(triInfo);
                                    if (ret) {
                                        window.WfForm.showMessage(ret, 1, 5);
                                    } else {
                                        callback();
                                    }
                                });
                            }
                            //传阅时
                            else if (action == 58) {
                                window.WfForm.registerCheckEvent(window.WfForm.OPER_TURNREAD, function(callback){
                                    let ret = window.formulaTrigger(triInfo);
                                    if (ret) {
                                        window.WfForm.showMessage(ret, 1, 5);
                                    } else {
                                        callback();
                                    }
                                });
                            }
                        }
                    });
                }
            } catch(ex) {
                if(window.console)	console.log("Formula calculate error: "+ex);
            }
        });
    }
}

//新增和删除明细行时执行公式
export const calculateDetailFormulaNew = (triggerTableMark, rowIndex) => {
    const { dataJson,fieldAttrMap,mainData,detailMap,linkageCfg} = src.layoutStore;
    const { formulas,systemdatas} = linkageCfg.formulaCfg;
    if (formulas) {
        formulas.map(data => {
            //formula:"[sys.-1]=1",formuladesc:"[系统.请求标题]=1",formulakey:"1533908594972#new#",
            //name:"",triggerdesc:"值改变时 (触发字段: 主表.申请人)##载入时",triggers:"1#21934##21",
            try {
                var newRowIndex = rowIndex.split(",");
                for (var i = 0; i < newRowIndex.length; i++) {
                    var oneRowIndex = newRowIndex[i];
                    //1#21934##21
                    const triggers = data.triggers;
                    if (triggers) {
                        //1#21934
                        //21
                        let triggersArray = triggers.split("##");
                        triggersArray && triggersArray.map(trigger => {
                            let triggerArray = trigger.split("#");
                            if (triggerArray && triggerArray.length > 0 && triggerArray[0]) {
                                //触发动作
                                const action = triggerArray[0];
                                if (action == 1 || action == 2 || action == 3 || action == 4 ||
                                    action == 5 || action == 6 || action == 7 || action == 21) {
                                    //触发字段
                                    let tableMark = "";
                                    if (triggerArray.length > 1) {
                                        tableMark = fieldAttrMap.get(triggerArray[1]).tableMark;
                                    }

                                    if (triggerTableMark == tableMark
                                        || (data.formulatxt && data.formulatxt.indexOf(triggerTableMark + ".") > -1)
                                        || (data.formula && data.formula.indexOf(triggerTableMark + ".") > -1)
                                        || (data.destcell && data.destcell.indexOf(triggerTableMark + ".") > -1)
                                        || (data.destfield && data.destfield.indexOf(triggerTableMark + ".") > -1)
                                        || (data.cellrange && data.cellrange.indexOf(triggerTableMark + ".") > -1)) {
                                        let triInfo = {};
                                        if (data.formulatxt) {
                                            triInfo.formulatxt = eval(data.formulatxt);
                                        }
                                        triInfo.formulakey = data.formulakey;
                                        if (data.destcell) {
                                            triInfo.destcell = eval(data.destcell);
                                        }
                                        if (data.destfield) {
                                            triInfo.destfield = eval(data.destfield);
                                        }
                                        if (data.cellrange) {
                                            triInfo.cellrange = eval(data.cellrange);
                                        }

                                        let triggerGroupid;
                                        if (triggerTableMark && triggerTableMark.indexOf("_") >= 0) {
                                            triggerGroupid = triggerTableMark.substring(triggerTableMark.indexOf("_") + 1);
                                        }
                                        triInfo.triggerGroupid = triggerGroupid;
                                        triInfo.systemdata = systemdatas;

                                        triInfo.workflowid = src.globalStore.commonParam.workflowid;
                                        triInfo.formid = src.globalStore.commonParam.formid;
                                        triInfo.isbill = src.globalStore.commonParam.isbill;
                                        triInfo.requestid = src.globalStore.commonParam.requestid;
                                        triInfo.triggerRowIndex = oneRowIndex;
                                        //执行公式
                                        window.formulaTrigger(triInfo);
                                    }
                                }
                            }
                        })
                    }
                }
            } catch(ex) {
                if(window.console)	console.log("Formula calculate error: "+ex);
            }
        })
    }
}
